package com.higold.cloud.ui.adapter

import android.view.View
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.higold.cloud.R
import com.higold.cloud.mvp.model.bean.achievedetailbean.Current
import com.higold.cloud.view.UpdataAPPProgressBar
import com.higold.cloud.view.recyclerview.CurrentAgentItem
import java.text.DecimalFormat

/**
 *    author : lxz
 *    date   : 2019/8/26 19:55
 *    desc   :
 */
class AgentDetailAdapter(data: List<MultiItemEntity>) : BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder>(data) {

    companion object {
        val TYPE_LEVEL_0 = 0
        val TYPE_LEVEL_1 = 1
    }

    init {
        addItemType(TYPE_LEVEL_0, R.layout.item_agent_achieved)
        addItemType(TYPE_LEVEL_1, R.layout.item_achieved)
    }


    override fun convert(holder: BaseViewHolder, item: MultiItemEntity) {
        when (holder.itemViewType) {
            TYPE_LEVEL_0 -> {
                when (holder.layoutPosition) {
                    0 -> holder.setVisible(R.id.iv_divider, false)
                    else -> holder.setVisible(R.id.iv_divider, true)
                }
                val agent = item as CurrentAgentItem
                holder.setText(R.id.tv_agent, "五金事业部：")
                        .setText(R.id.tv_goal, changeFloatToString(agent.data.amount) + "w / "
                                + changeFloatToString(agent.data.mission_amount) + "w本月")
                        .setText(R.id.tv_percent, changeFloatToString(agent.data.rate) + "%")
//                        .setImageResource(R.id.iv, if (lv0.isExpanded) R.mipmap.arrow_b else R.mipmap.arrow_r)
                val progress = (holder.getView<View>(R.id.pg_manager) as UpdataAPPProgressBar)
                progress.progress = agent.data.rate.toFloat()
                progress.duration = 1500
                progress.startAnim()

                holder.itemView.setOnClickListener {
                    val pos = holder.adapterPosition
                    if (agent.isExpanded) {
                        collapse(pos)
                    } else {
                        expand(pos)
                    }
                }
            }

            TYPE_LEVEL_1 -> {
                val agentCurrent = item as Current
                holder.setText(R.id.tv_agent, agentCurrent.remark)
                        .setText(R.id.tv_goal, changeFloatToString(agentCurrent.amount) + "w / "
                                + changeFloatToString(agentCurrent.mission) + "w本月")
                        .setText(R.id.tv_percent, changeFloatToString(agentCurrent.amount / agentCurrent.mission) + "%")
//                        .setImageResource(R.id.iv, if (lv1.isExpanded()) R.mipmap.arrow_b else R.mipmap.arrow_r)
                val progress = (holder.getView<View>(R.id.pg_manager) as UpdataAPPProgressBar)
                progress.progress = (agentCurrent.amount / agentCurrent.mission).toFloat()
                progress.duration = 1500
                progress.startAnim()

                holder.itemView.setOnClickListener {
                    val pos = holder.adapterPosition
                }
            }
        }
    }

    private fun changeFloatToString(double: Double): String {
        val decimalFormat = DecimalFormat("##.##")
        return decimalFormat.format(double)
    }

}